import 'package:flutter/material.dart';
import 'package:qunaimedical/components/custom_avatar.dart';
import 'package:qunaimedical/models/doctor.dart';
import 'package:qunaimedical/pages/doctor_simple_detail.dart';
import 'package:qunaimedical/utils/event_bus_manager.dart';

class ItemDoctor extends StatelessWidget {
  final Doctor item;
  final bool showCheckbox;
  final GestureTapCallback onCheckboxTap;
  final VoidCallback onFavorite;
  final GestureTapCallback onTap;

  const ItemDoctor(this.item,
      {Key key,
      this.showCheckbox,
      this.onCheckboxTap,
      this.onFavorite,
      this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap ??
          () {
            Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (c) =>
                            DoctorSimpleDetailPage(doctorId: item.id)))
                .then((value) {
              eventBus.fire(EventBusManager(EventBusManager.favoriteRefresh));
            });
          },
      child: Container(
        color: Colors.white,
        child: Stack(
          children: [
            Positioned(
              right: 20,
              top: 20,
              child: Offstage(
                offstage: !(showCheckbox ?? false),
                child: Text(
                  item.whetherSelected == '0' ? '未约' : '已约',
                  style: TextStyle(
                      color: item.whetherSelected == '0'
                          ? Theme.of(context).primaryColor
                          : Colors.blue[200]),
                ),
              ),
            ),
            Positioned(
              right: 0,
              top: 0,
              bottom: 0,
              child: Offstage(
                offstage:
                    !(showCheckbox ?? false) || item.whetherSelected == '1',
                child: CustomCheckBoxState(
                  value: item.selected,
                  onTap: onCheckboxTap,
                ),
              ),
            ),
            Column(
              children: [
                Container(
                  padding: EdgeInsets.only(right: 16, bottom: 16),
                  child: Row(
                    children: [
                      Column(
                        children: [
                          CustomAvatar(
                            size: 60,
                            margin: EdgeInsets.only(
                                left: 16, top: 5, right: 16, bottom: 8),
                            avatarUrl: item.avatar,
                          ),
                          OutlinedButton(
                            onPressed: onFavorite,
                            child: Text(
                                item.whetherAppointed == '0' ? '收藏' : '已收藏',
                                style: TextStyle(color: Colors.grey)),
                            style: ButtonStyle(
                              shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                              ),
                              minimumSize:
                                  MaterialStateProperty.all(Size(50, 28)),
                              elevation: MaterialStateProperty.all(0),
                            ),
                          )
                        ],
                      ),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(
                              padding:
                                  const EdgeInsets.only(top: 8.0, right: 40),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Row(
                                      children: [
                                        Container(
                                          constraints:
                                              BoxConstraints(minWidth: 70),
                                          child: Text(
                                            item.doctorName ??
                                                item.fullname ??
                                                '',
                                            softWrap: false,
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                                fontSize: 16,
                                                fontWeight: FontWeight.bold),
                                          ),
                                        ),
                                        Padding(
                                          padding: const EdgeInsets.all(8.0),
                                          child: Text(item.sectionName ?? ''),
                                        ),
                                        Text(item.professionalName ?? ''),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.only(top: 4.0),
                              child: Text(item.office ?? ''),
                            ),
                            Container(
                              padding: const EdgeInsets.symmetric(vertical: 8),
                              height: 55,
                              child: Text(
                                item.skilled ?? '',
                                style: Theme.of(context).textTheme.caption,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            Divider(),
                            Padding(
                              padding: const EdgeInsets.only(top: 4.0),
                              child: Text(
                                '已有 ${item.appointedNum} 人预约过',
                                style: Theme.of(context).textTheme.caption,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                Divider(),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class CustomCheckBoxState extends StatelessWidget {
  final bool value;
  final GestureTapCallback onTap;

  const CustomCheckBoxState({Key key, this.value, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
          onTap: onTap,
          behavior: HitTestBehavior.opaque,
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: (value ?? false)
                ? Icon(
                    Icons.check_circle_outline,
                    size: 24.0,
                    color: Theme.of(context).primaryColor,
                  )
                : Icon(
                    Icons.panorama_fish_eye,
                    size: 24.0,
                    color: Colors.grey,
                  ),
          )),
    );
  }
}
